package edu.stanford.jgc;

import edu.stanford.jgc.GeocoderWrapper.GeocoderWrapper;
import edu.stanford.jgc.datacollector.DataCollector;
import edu.stanford.jgc.datacollector.DataCollector.Type;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class JGCActivity extends Activity {
	
    private EditText editTextActivity;
    private EditText editTextAddress;
    private EditText editTextURL;
    private EditText editTextAgeMin;
    private EditText editTextAgeMax;
    private Button buttonSubmit;
    private Button buttonDaySelector;
    private Spinner spinnerActivityType;
    private Spinner spinnerStartTime;
    private Spinner spinnerEndTime;
    private CheckBox checkBoxAnyAge;
	GeocoderWrapper coder;
	DataCollector dc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        coder = new GeocoderWrapper(new Geocoder(this));
        dc = new DataCollector(coder);
        
        setContentView(R.layout.main);
        
        // Add pointers to UI objects.
        editTextActivity = (EditText) findViewById(R.id.editTextActivity);
        editTextAddress = (EditText) findViewById(R.id.editTextAddress);
        editTextURL = (EditText) findViewById(R.id.editTextURL);
        editTextAgeMin = (EditText) findViewById(R.id.editTextAgeMin);
        editTextAgeMax = (EditText) findViewById(R.id.editTextAgeMax);
        
        checkBoxAnyAge = (CheckBox) findViewById(R.id.checkBoxAnyAge);
        
        buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
        buttonDaySelector = (Button) findViewById(R.id.buttonDaySelector);
        
        spinnerActivityType = (Spinner) findViewById(R.id.spinnerActivityType);
        spinnerStartTime = (Spinner) findViewById(R.id.spinnerStartTime);
        spinnerEndTime = (Spinner) findViewById(R.id.spinnerEndTime);
        
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.activity_prompt, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerActivityType.setAdapter(adapter);

//      spinnerActivityType.setAdapter(new ArrayAdapter<Type>(this, android.R.layout.simple_list_item_1, Type.values()));
        
        
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
                this, R.array.time_prompt, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerStartTime.setAdapter(adapter2);
        spinnerEndTime.setAdapter(adapter2);
        
        setButtonListeners();
        
        
    }
    
	private void setButtonListeners() {
		
		checkBoxAnyAge.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(!isChecked) {
					editTextAgeMin.setVisibility(EditText.VISIBLE);
					editTextAgeMax.setVisibility(EditText.VISIBLE);
				}
				else {
					editTextAgeMin.setVisibility(EditText.INVISIBLE);
					editTextAgeMax.setVisibility(EditText.INVISIBLE);
				}
			}

		});

		buttonDaySelector.setOnClickListener(new OnClickListener(){
			public void onClick(View w) {
				
				new AlertDialog.Builder(JGCActivity.this)
				.setTitle("Select Activity Days")
				.setMultiChoiceItems(new String[]{"Sun", "Mon","Tue","Wed","Thu", "Fri","Sat"},
						new boolean[]{false, false, false, false, false, false, false},
						new DialogInterface.OnMultiChoiceClickListener() {
					public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {

						/* User clicked on a check box do some stuff */
					}
				})
				.setPositiveButton("Done",
						new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {

						/* User clicked done so do some stuff */
						
					}
				})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {

						/* User clicked No so do some stuff */
					}
				})
				.create().show();
			}
		});

		buttonSubmit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dc.setActivityName(editTextActivity.getText().toString());
				dc.setURL(editTextURL.getText().toString());
				dc.setAddress(editTextAddress.getText().toString());
				dc.setStartTime(spinnerStartTime.getSelectedItem().toString());
				dc.setEndTime(spinnerEndTime.getSelectedItem().toString());
				
			}
		});

	}

}